package com.muatocroi.tornado;

import java.util.Timer;
import java.util.TimerTask;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ToggleButton;

public class ProcessSlideShowControl2 implements OnTouchListener {
	private static final int INVALID_POINTER_ID = -1;
	static float lastMoveX, lastMoveY;
	static float lastScrollX, lastScrollY;
	static long downTime;
	static boolean isDowning = false;
	static boolean isScrolling = false;
	static Timer timerWaitClick = null;
	static SignalSender sender = null;
	static ProcessSlideShowControl2 _instance = null;
	static boolean isWaitingClick = false;
	static int idLButton = INVALID_POINTER_ID;
	static int idRButton = INVALID_POINTER_ID;
	static int idMoving = INVALID_POINTER_ID;
	static int idScrolling = INVALID_POINTER_ID;
	static ViewTouchPad viewer = null;

	ToggleButton tbtnLazer;
	
	private ProcessSlideShowControl2() {
	}

	public static ProcessSlideShowControl2 instance() {
		if (_instance == null)
			_instance = new ProcessSlideShowControl2();
		return _instance;
	}

	public static void setSender(SignalSender sender) {
		ProcessSlideShowControl2.sender = sender;
	}
	public static void setTouchView(View v) {
		v.setOnTouchListener(ProcessSlideShowControl2.instance());
		viewer = (ViewTouchPad) v;
	}

	@Override
	public boolean onTouch(View v, MotionEvent e) {

		int actIndex = e.getActionIndex();
		int x = (int) e.getX(actIndex);
		int y = (int) e.getY(actIndex);
		int pid = e.getPointerId(actIndex);

		switch (e.getAction() & MotionEvent.ACTION_MASK) {
         
			case MotionEvent.ACTION_DOWN:
			case MotionEvent.ACTION_POINTER_1_DOWN:
			case MotionEvent.ACTION_POINTER_2_DOWN:
			case MotionEvent.ACTION_POINTER_3_DOWN:
				if (viewer.rectMove.contains(x, y)) {
					idMoving = pid;
					lastMoveX = x;
					lastMoveY = y;
				} else
				if (viewer.rectScroll.contains(x, y)) {
					idScrolling = pid;
					if (!viewer.isScrolling) {
						viewer.isScrolling = true;
						viewer.invalidate();
					}
					lastScrollY = y;
					idScrolling = pid;
				} else if (viewer.rectLButton.contains((int) x, (int) y)) {
					idLButton = pid;
					if (!viewer.isLeftDowning) {
						viewer.isLeftDowning = true;
						viewer.invalidate();
					}
				} else if (viewer.rectRButton.contains((int) x, (int) y)) {
					idRButton = pid;
					if (!viewer.isRightDowning) {
						viewer.isRightDowning = true;
						viewer.invalidate();
					}
				}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_POINTER_1_UP:
			case MotionEvent.ACTION_POINTER_2_UP:
			case MotionEvent.ACTION_POINTER_3_UP:
				if (pid == idMoving) {
					float dis = x - lastMoveX;
					if ( dis > 150){
						sender.Send(CommandPowerpoint.Control(VirtualKeyCode.LEFT));
					}
					else if (dis < -150){
						sender.Send(CommandPowerpoint.Control(VirtualKeyCode.RIGHT));
					}
					
					idMoving = INVALID_POINTER_ID;
				}
				else if (pid == idLButton) {
					sender.Send(CommandPowerpoint.Control(VirtualKeyCode.LEFT));
					viewer.isLeftDowning = false;
					viewer.invalidate();
					idLButton = INVALID_POINTER_ID;
				} else if (pid == idRButton) {
					sender.Send(CommandPowerpoint.Control(VirtualKeyCode.RIGHT));
					viewer.isRightDowning = false;
					viewer.invalidate();
					idRButton = INVALID_POINTER_ID;
				} else if (pid == idScrolling) {
					viewer.isScrolling = false;
					viewer.invalidate();
					idScrolling = INVALID_POINTER_ID;
				}
				break;
	
			case MotionEvent.ACTION_MOVE:
				int c = e.getPointerCount();
				for (int p = 0; p < c; p++)
				{
					pid = e.getPointerId(p);
					y = (int) e.getY(p);
					if (pid == idScrolling)
					{
						short sline = (short) ((lastScrollY - y) * 0.5);
						sender.Send(CommandMouse.VScroll((byte) sline));
						lastScrollY = y;
						break;
					}
				}
				break;
		}

		return true;
	}
}
